﻿@model M2B.GestionEntretiens.POCOS.Entretien
@using System.Globalization
@using M2B.GestionEntretiens.Helpers.HTML
@using M2B.GestionEntretiens.POCOS



<script src="~/Scripts/jquery.validate.min.js"></script>
<script src="~/Scripts/jquery.validate.unobtrusive.min.js"></script>
<script src="~/Scripts/Pager/quickpager.jquery.js"></script>

<style>
    label {
        margin: 0;
        padding: 0;
        font-size: 14px;
        margin-left: 5px;
        width: 100px;
    }

</style>


<script>
    var candidatId = "@Model.Candidat.CandidatId";
    var testId = "@Model.Test.TestId";
    var typeTest = "@Model.Test.Type";
    debugger;
    function SaveUpdateEntretien(event) {

        var form = $('#formCandidat');
        //Validation des données
        if (form.valid() && candidatId != 0 && testId != 0) {
            var drpStatuts = document.getElementById("drpStatuts");
            var drpLangues = document.getElementById("drpLangue");
            debugger;
            var entretien = form.serializeObject();
            entretien.DateEntretien = $("#dateEntretien").datepicker("getDate");
            entretien.Langue = drpLangues.options[drpLangues.selectedIndex].value;
            entretien.IsValid = drpStatuts.options[drpStatuts.selectedIndex].value=="1";
            entretien.Test = {};
            entretien.Test.TestId = testId;
            entretien.Candidat = {};
            entretien.Candidat.CandidatId = candidatId;

            $.ajax({
                url: '@Url.Action("Edit")',
                type: 'POST',
                data: $.postify(entretien),
                success: function () {
                    window.location.href = '@Url.Action("Index")';
                }
            });


        }
    }
    function TypeTestChanged() {
        var drpTypeTest = document.getElementById("drpTypeTest");
        typeTest = drpTypeTest.options[drpTypeTest.selectedIndex].value;

        $("#TestLiveSearch").val("");
        testId = 0;

    }

</script>


@{
    //Initialisation de la date de l'entretien
    var shortDateDateEntretien = string.Empty;
    if (Model.DateEntretien != null)
    {
        shortDateDateEntretien = ((DateTime)Model.DateEntretien).ToShortDateString();
    }
    var candidatEntretienLabel = string.Empty;
    if (Model.Candidat != null)
    {
        candidatEntretienLabel = Model.Candidat.Prenom + " " + Model.Candidat.Nom;
    } 
    var testEntertienLabel = string.Empty;
    if (Model.Test != null)
    {
        testEntertienLabel = Model.Test.Libelle;
    }
    var langueEntretiens = (List<Referentiel>)ViewData["LangueEntretiens"];
    var langues = langueEntretiens.Select(item => new SelectListItem { Text = item.Valeur, Value = item.Code, Selected = (Model.Langue.ToString(CultureInfo.InvariantCulture) == item.Code) }).ToList();
    var statutsEntretiens = (List<Referentiel>)ViewData["StatutsEntretiens"];
    var statuts = statutsEntretiens.Select(item => new SelectListItem { Text = item.Valeur, Value = item.Code, Selected = (Model.Statut.ToString(CultureInfo.InvariantCulture) == item.Code) }).ToList();
    var typeTests = (List<Referentiel>)ViewData["TypeTests"];
    var typeTest = typeTests.Select(item => new SelectListItem { Text = item.Valeur, Value = item.Code, Selected = (Model.Test.Type.ToString(CultureInfo.InvariantCulture) == item.Code)}).ToList();

}
<script>
    //Date de l'entretien
    $(function () {
        $("#dateEntretien").datepicker({ dateFormat: 'dd-mm-yy' });
    });
    //Autocomplete pour la liste des candidats
    $(function () {

        $("#CandidatLiveSearch").autocomplete({
            minLength: 1,
            cacheLength: 1,
            source: function (request, response) {
                $.getJSON("/Entretiens/SearchCandidat?textSearch=" + request.term, function (data) {
                    response($.map(JSON.parse(data), function (candidat) {
                        return {
                            Prenom: candidat.Prenom + " " + candidat.Nom,
                            CandidatId: candidat.CandidatId
                        };
                    }));
                });
            },
            select: function (event, ui) {
                $("#CandidatLiveSearch").val(ui.item.Prenom);
                candidatId = ui.item.CandidatId;
                return false;
            },

        })
             .data("ui-autocomplete")._renderItem = function (ul, item) {

                 return $("<li >")
                     .append("<a>" + item.Prenom + "<br>" + "</a>")
                     .appendTo(ul);
             };

        $(".ui-autocomplete").css("zIndex", parseInt($("[style*=z-index]:last").css("zIndex"), 10) + 2);
    });
    //Autocomplete pour la liste des tests
    $(function () {

        $("#TestLiveSearch").autocomplete({
            minLength: 1,
            cacheLength: 1,
            source: function (request, response) {

                $.getJSON("/Entretiens/SearchTest?textSearch=" + request.term + "&typeTest=" + typeTest, function (data) {
                    response($.map(JSON.parse(data), function (test) {
                        return {
                            Libelle: test.Libelle,
                            TestId: test.TestId
                        };
                    }));
                });
            },
            select: function (event, ui) {
                $("#TestLiveSearch").val(ui.item.Libelle);
                testId = ui.item.TestId;
                return false;
            }
        })
             .data("ui-autocomplete")._renderItem = function (ul, item) {

                 return $("<li >")
                     .append("<a>" + item.Libelle + "<br>" + "</a>")
                     .appendTo(ul);
             };

        $(".ui-autocomplete").css("zIndex", parseInt($("[style*=z-index]:last").css("zIndex"), 10) + 2);
    });

    $(function () {

        var $elem = $('#CandidatLiveSearch');

        $elem.on('blur', function () { // Focus event
           
            if (candidatId != 0 && this.value != '') {

                $(this).removeClass("errorClass");
            }
            else if (candidatId != 0 || this.valueOf == '') {

                candidatId = 0;
                $(this).addClass("errorClass");
            }

        });
    });

    $(function () {

        var $elem = $('#TestLiveSearch');

        $elem.on('blur', function () { // Focus event
          
            if (testId != 0 && this.value != '') {

                $(this).removeClass("errorClass");
            }
            else if (testId != 0 || this.valueOf == '') {
                testId = 0;
                $(this).addClass("errorClass");
            }

        });
    });
</script>

@using (Html.BeginForm("", "", FormMethod.Post, new { id = "formCandidat" }))
{
    @Html.AntiForgeryToken()
    @Html.ValidationSummary(false)

    <fieldset style="border: #C5DBEC solid 1px;">
        <legend>Candidat</legend>
        @Html.HiddenFor(model => model.EntretienId)
        <table>
            <tr>
                <td>
                    @Html.LabelFor(model => model.Titre, "Titre")
                </td>
                <td>
                    @Html.TextBoxFor(model => model.Titre, new { @style = "float:left;" })
                    @Html.ValidationMessageFor(model => model.Titre, "*", new { @style = "float:left;margin-left: 5px;" })
                </td>
            </tr>
            <tr>
                <td>
                    @Html.LabelFor(model => model.DateEntretien, "Date")
                </td>
                <td>
                    <input type="text" id="dateEntretien" value="@shortDateDateEntretien" class="DatePicker">
                    @Html.ValidationMessageFor(model => model.DateEntretien, "*", new { @style = "float:left;margin-left: 5px;" })
                </td>
            </tr>

            <tr>
                <td>@Html.LabelFor(model => model.Langue)</td>
                <td>
                    <div>
                        <table>
                            <tr>
                                <td>
                                    @Html.DropDownList("Value", langues, new { @class = "DropDownList", @id = "drpLangue" })

                                </td>
                                <td>
                                    @Html.LabelFor(model => model.Statut)
                                </td>
                                <td>
                                    @Html.DropDownList("Value", statuts, new { @class = "DropDownList", @id = "drpStatuts" })

                                </td>

                        </table>
                    </div>
                </td>
            </tr>

            <tr>
                <td>
                    <label>Type Test</label>
                </td>
                <td>
                    <div>
                        <table>
                            <tr>
                                <td>
                                    @Html.DropDownList("Value", typeTest, new { @class = "DropDownList", @id = "drpTypeTest", @onchange = "TypeTestChanged()" })
                                </td>
                                <td>
                                    <label>Test</label>
                                </td>
                                <td>
                                    <input type="text" id="TestLiveSearch" value="@testEntertienLabel" />
                                </td>

                        </table>
                    </div>
                </td>
            </tr>
            <tr>
                <td>
                    Candidat
                </td>
                <td>
                    <input type="text" id="CandidatLiveSearch" value="@candidatEntretienLabel" />

                </td>

            </tr>
            <tr>
                <td>
                    @Html.LabelFor(model => model.Commentaire, "Commentaire")
                </td>
                <td>
                    @Html.TextAreaFor(model => model.Commentaire, new { @class = "TextArea" })

                </td>

            </tr>
        </table>



        <div style="float: right">
            <div>
                <table>
                    <tr>
                        <td>
                            @Html.ActionImage("", "", "~/Content/Images/Save.png", "Sauvegarder", "32px", "32px", "SaveUpdateEntretien(event)")
                            @Html.ActionImage("Index", "", "~/Content/Images/Cancel.png", "Annuler", "32px", "32px", "")
                        </td>
                    </tr>
                </table>
            </div>


        </div>
    </fieldset>

}



